package com.ciwong.epaper.modules.reciteWords.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.ciwong.epaper.R;

import java.lang.reflect.Field;
import java.util.List;

public class FragmentAdapter extends FragmentPagerAdapter {

    private final List<Fragment> list;
    private Context mContext = null;

    private String[] titles = {"已学单词", "未学单词"};
    public FragmentAdapter(FragmentManager fm, Context context, List<Fragment> list) {
        super(fm);
        this.mContext = context;
        this.list = list;
    }

    @Override
    public Fragment getItem(int position) {
        return list.get(position);
    }

    @Override
    public int getCount() {
        return list==null?0:list.size();
    }

 /*   @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }*/
    /**
     * 自定义方法，提供自定义Tab
     *
     * position 位置
     * return返回Tab的View
     */
    public View getTabView(int position) {
        TextView mTv_Title  =  (TextView) LayoutInflater.from(mContext).inflate(R.layout.tab_custom, null);
//        TextView mTv_Title = (TextView) v.findViewById(R.id.mTv_Title);
//        ImageView mImg = (ImageView) v.findViewById(R.id.mImg);
        Drawable drawable = null;
        int resId=-1;
        if (position==0){
            resId=R.drawable.select_tab_icon_learned;
        }else{
            resId=R.drawable.select_tab_icon_unlearn;
        }
        drawable = mContext.getResources().getDrawable(resId);
        drawable.setBounds(0, (int) dp2px(1), drawable.getMinimumWidth(), drawable.getMinimumHeight());
        mTv_Title.setCompoundDrawables(drawable,null,null,null);
        mTv_Title.setCompoundDrawablePadding((int) dp2px(2));
        mTv_Title.setText(titles[position]);
//        mImg.setImageResource(images[position]);
        //添加一行，设置颜色
//        mTv_Title.setTextColor(mTabLayout.getTabTextColors());//
        return mTv_Title;
    }
    public float dp2px(float dp)
    {
        final float scale = mContext.getResources().getDisplayMetrics().density;
        return dp * scale + 0.5f;
    }

    /**
     * 设置indicator 的长度
     * @param tabs TabLayout控件
     * @param leftDip 左边 margin值(dp)
     * @param rightDip 右边margin值(dp)
     */
    public void setIndicator(TabLayout tabs, int leftDip, int rightDip) {
        Class<?> tabLayout = tabs.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayout.getDeclaredField("mTabStrip");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        if (tabStrip==null)return;
        tabStrip.setAccessible(true);
        LinearLayout llTab = null;
        try {
            llTab = (LinearLayout) tabStrip.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        if (llTab==null)return;
        int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp2px(leftDip), Resources.getSystem().getDisplayMetrics());
        int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp2px( rightDip), Resources.getSystem().getDisplayMetrics());

        for (int i = 0; i < llTab.getChildCount(); i++) {
            View child = llTab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            child.setLayoutParams(params);
            child.invalidate();
        }
    }
}
